package mobile.android.Hotel;

import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import com.google.android.maps.Overlay;

import java.io.IOException;
import java.util.List;
import java.util.Locale;


import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Map extends MapActivity {
	
	MapView mapView;
	MapController mc;
	GeoPoint p;

	class MapOverlay extends com.google.android.maps.Overlay
	{
		@Override
		public boolean draw(Canvas canvas, MapView mapView,boolean shadow, long when)
		{
			super.draw(canvas, mapView, shadow);
			Point screenPts = new Point();
			mapView.getProjection().toPixels(p,screenPts);
			
			Bitmap bmp=BitmapFactory.decodeResource(getResources(),R.drawable.pin);
			canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50,null);
			return true;
		}
		@Override
		public boolean onTouchEvent(MotionEvent event,MapView mapView)
		{
			if(event.getAction()==1){
				GeoPoint p=mapView.getProjection().fromPixels(
				(int) event.getX(),
				(int) event.getY());
				
				
				//Toast.makeText(getBaseContext(),"Location,"+p.getLatitudeE6()/1E6+","+p.getLongitudeE6()/1E6, Toast.LENGTH_SHORT).show();
				Geocoder geoCoder=new Geocoder(getBaseContext(),Locale.getDefault());
				
				try {
                    List<Address> addresses = geoCoder.getFromLocation(
                        p.getLatitudeE6()  / 1E6, 
                        p.getLongitudeE6() / 1E6, 1);
 
                    String add = "";
                    if (addresses.size() > 0) 
                    {
                        for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); 
                             i++)
                           add += addresses.get(0).getAddressLine(i) + "\n";
                    }
 
                    Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
                }
                catch (IOException e) {                
                    e.printStackTrace();
                }
			}
				return false;
		}
	}

	@Override
public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        
        mapView = (MapView) findViewById(R.id.map_view);
        mapView.setBuiltInZoomControls(true);
        //mapView.setStreetView(true);
        
        mc = mapView.getController();
        Bundle extras  = getIntent().getExtras();
        final String Add=extras.getString("add");
        Geocoder geoCoder=new Geocoder(this,Locale.getDefault());
		try{
			List<Address> addresses=geoCoder.getFromLocationName(Add, 1);
			//String add="";
			if(addresses.size()>0){
				p= new GeoPoint(
						(int) (addresses.get(0).getLatitude()*1E6),
						(int) (addresses.get(0).getLongitude()*1E6));
			}
		}
		catch (IOException e){
			String coordinates[]={"16.051564","108.214892"};
	        double lat = Double.parseDouble(coordinates[0]);
	        double lng = Double.parseDouble(coordinates[1]);
	        
	        p=new GeoPoint(
	        		(int)(lat*1E6),
	        		(int)(lng*1E6));
		}
        
        mc.animateTo(p);
        mc.setZoom(17);
        
        MapOverlay mapOverlay=new MapOverlay();
        List<Overlay> listofOverlays=mapView.getOverlays();
        listofOverlays.clear();
        listofOverlays.add(mapOverlay);
        mapView.invalidate();
        
        Button btn = (Button)findViewById(R.id.btnOK);
        btn.setOnClickListener(new View.OnClickListener() {
	        public void onClick(View v){
	        	EditText ed = (EditText)findViewById(R.id.editDD);
	            String Ten = ed.getText().toString();
	            Geocoder geoCoder=new Geocoder(getBaseContext(),Locale.getDefault());
	    		try{
	    			List<Address> addresses=geoCoder.getFromLocationName(Ten, 1);
	    			if(addresses.size()>0){
	    				p= new GeoPoint(
	    						(int) (addresses.get(0).getLatitude()*1E6),
	    						(int) (addresses.get(0).getLongitude()*1E6));
	    				mc.animateTo(p);
	    		        mc.setZoom(17);
	    		        
	    		        MapOverlay mapOverlay=new MapOverlay();
	    		        List<Overlay> listofOverlays=mapView.getOverlays();
	    		        listofOverlays.clear();
	    		        listofOverlays.add(mapOverlay);
	    		        mapView.invalidate();
	    			}
	    		}
	    		catch (IOException e){
	    			
	    		}
	        }
        });
               
	
	}
	 
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}//extends MapActivity{

}